//
// Created by wll on 19-6-30.
//

#include "Compiler.h"
#include "WllInterpreter.h"
#include "CompilerId.h"
#include "LanguageGrammar.h"
#include "LanguageParsers.h"
#include "Strategy.h"
#include "SmartPointer.h"
#include <string>
#include <map>
#include <WllUtil.h>
#include <WllTraceConfig.h>
#include <iostream>
#include <vector>

#ifndef XYZ_COMPILERMANAGER_H
#define XYZ_COMPILERMANAGER_H

//前置声明Compiler,因为有循环include
class Compiler;
class WllInterpreter;

class CompilerManager {
public:
    CompilerManager();
    ~CompilerManager();
    void LoadSymbols();
    void SaveSymbols();



public:
    Compiler* GetCompiler(string compiler_grammar_file_name);
    Compiler* CreateCompiler(string compiler_grammar_file_name, Compiler *parent_compiler);
    string GetBoostrapCompilerFileName();
    Compiler* CreateOrGetBoostrapCompiler();
    Compiler* CreateOrGetCompiler(std::string compiler_grammar_file_name, Compiler* parent_compiler = nullptr);
    void CacheCompiler(Compiler *compiler, bool skip_disk_cache = false);
private:
    //compiler_id => Compiler*
    map<string, Compiler*> compiler_map;
    Compiler* LookupCompiler(string compiler_id);
public:
    //grammar_file_name -> compiler_id
    CompilerId * LookupGrammarId(string grammar_file_name);
    //grammar_file_name -> compiler_id
    CompilerId * GetGrammarId(string grammar_file_name);
private:
    //grammar_file_name => compiler_id
    map<string, CompilerId> file_name_to_compiler_id;

//当前编译器
public:
    //获取当前编译器
    Compiler* GetCurrentCompiler() { return this->current_compiler; }
    //设置当前编译器
    void SetCurrentCompiler(Compiler* compiler) { this->current_compiler = compiler; }
private:
    Compiler* current_compiler;

//缓存策略管理
public:
    //内存缓存开启
    void EnableMemoryCache() { this->memory_cache_flag = true; }
    //内存缓存关闭
    void DisableMemoryCache() { this->memory_cache_flag = false; }
    //磁盘缓存开启
    void EnableDiskCache() { this->disk_cache_flag = true; }
    //磁盘缓存关闭
    void DisableDiskCache() { this->disk_cache_flag = false; }
private:
    bool memory_cache_flag;
    bool disk_cache_flag;
    string language_parser_type;
    int wll_interpreter_version;
    int wll_grammar_version;
public:
    static const char* TOPDOWNPARSER;
    static const char* LR1PARSER;
private:
    void LoadConfig();

public:
    static string GetCacheBaseDir();

    static string GetCacheWllFileName(string compiler_id);

    static string GetSymbolTableFileName();

    static string GetCacheLR1FileName(string compiler_id);

    static LanguageParsers *CreateLanguageParser(const string& string, Compiler* compiler);

    WllInterpreter* CreateWllInterpreter(Compiler *compiler);
    WllInterpreter* GetOrCreateWllInterpreter(Compiler *compiler);

    Compiler *LoadCompiler(CompilerId *compiler_id);

};

#endif //XYZ_COMPILERMANAGER_H
